<template>
  <div class="detail-container">
    <el-card class="box-card">
      <el-tabs v-model="activeName">
        <!-- 账号登录 -->
        <el-tab-pane label="登录账号设置" name="first">
          <el-form ref="form" :rules="rules" :model="form" label-width="300px">
            <el-form-item prop="username" label="姓名">
              <el-input v-model="form.username" style="width:50%" />
            </el-form-item>

            <el-form-item prop="password" label="密码">
              <el-input
                v-model="form.password"
                style="width:50%"
                type="password"
              />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm">更新</el-button>
              <el-button @click="createback">取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <!-- 个人详情 -->
        <el-tab-pane label="个人详情" name="second"><Info /></el-tab-pane>
        <!-- 岗位信息 -->
        <el-tab-pane label="岗位信息" name="third"><Job /></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getMoreInfo } from '../../api/user'
import { saveUserDatailById } from '../../api/employees'
import Info from './components/Info.vue'
import Job from './components/Job.vue'
export default {
  name: 'Detail',
  components: { Info, Job },
  data() {
    return {
      activeName: 'first',
      form: {
        username: '',
        password: ''
      },
      // 用户信息
      userInfo: {},
      // 表单验证
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度为6~9个字符', trigger: 'blur' }
        ]
      }
    }
  },
  // createx钩子
  async created() {
    const res = await getMoreInfo({ id: this.$route.params.id })
    // 保存用户数据
    this.userInfo = res
    console.log(res)
    // 修改表单的值
    this.form.username = res.username
  },
  methods: {
    // 取消按钮返回
    createback() {
      this.$router.back()
    },
    // 修改提交
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 提交成功
          await saveUserDatailById({ ...this.userInfo, ...this.form })
          this.$message.success('数据修改成功')
          this.$router.back()
        } else {
          // 提交失败
          this.$message.error('数据格式有误,请检查')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.detail-container {
  margin: 10px;
}
</style>
